#include "PayBox.h"
#include <Arduino.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <WiFi.h>

TFT_eSPI tft = TFT_eSPI();

void showTimeTFT(const String &string) {
  tft.setTextColor(TFT_PURPLE, TFT_BLACK);
  tft.drawCentreString(string, 160, 20, 7);
}

void showDateTFT(const String &string) {
  tft.setTextColor(TFT_CYAN, TFT_BLACK);
  tft.drawCentreString(string, 160, 100, 4);
}

void connectWifi() {
  tft.setTextColor(TFT_WHITE, TFT_BLACK);
  tft.setTextFont(2);
  tft.setCursor(0, 0);
  tft.println("Start Wifi Connect!");
  WiFi.mode(WIFI_MODE_STA);
  WiFi.begin();
  for (int i = 0; WiFi.status() != WL_CONNECTED && i < 100; i++) {
    delay(100);
  }
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.mode(WIFI_MODE_APSTA);
    WiFi.beginSmartConfig();
    tft.println("Connect With EspTouch!");
    while (WiFi.status() != WL_CONNECTED) {
      delay(100);
    }
    WiFi.stopSmartConfig();
    WiFi.mode(WIFI_MODE_STA);
  }
  tft.println(WiFi.localIP().toString());
  delay(1000);
  tft.fillScreen(TFT_BLACK);
}

void setup() {
  Serial.begin(115200);
  Serial.flush();
  payTFTInit(tft);
  payLEDInit();
  connectWifi();
}

void loop() {
  payLEDShow(0xf6229a);
  delay(1000);
  showTimeTFT("02:15:44");
  showDateTFT("2021-07-19");
  payLEDShow(0x33f177);
  delay(1000);
}